package com.group.Controller;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group.Enity.Building;
import com.group.Enity.CarCharge;
import com.group.Enity.Parking;
import com.group.Service.ParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("parking")
public class ParkingController {

    @Autowired
    private ParkingService parkingService;

    @ResponseBody
    @RequestMapping("queryParkAll")
    public JSONObject queryParkAll(Integer page,Integer limit,String numbers){

        PageHelper.startPage(page,limit);
        List<Parking> list = parkingService.getParks(numbers);
        PageInfo pageInfo = new PageInfo<>(list);
        Long len = pageInfo.getTotal();
        List<CarCharge> list_new = pageInfo.getList();

        JSONObject json=new JSONObject();
        json.accumulate("count",len);
        json.accumulate("code",0);
        json.accumulate("msg","null");
        json.accumulate("data",list_new);
        return json;

    }

    @ResponseBody
    @RequestMapping("add")
    public String add(@RequestBody JSONObject jsonObject){
        System.out.println(jsonObject);

        String numbers = jsonObject.getStr("numbers");
        int ownerID = jsonObject.getInt("ownerId");
        String remarks = jsonObject.getStr("remarks");

        Parking parking = new Parking();
        parking.setNumbers(numbers);
        parking.setOwner_id(ownerID);
        parking.setRemarks(remarks);

        parkingService.add(parking);

        return "";

    }

    @ResponseBody
    @RequestMapping("update")
    public String update(@RequestBody JSONObject jsonObject){

        int id = jsonObject.getInt("id");
        int ownerId = jsonObject.getInt("ownerId");
        String numbers = jsonObject.getStr("numbers");
        String remarks = jsonObject.getStr("remarks");

        Parking parking = new Parking();
        parking.setId(id);
        parking.setNumbers(numbers);
        parking.setOwner_id(ownerId);
        parking.setRemarks(remarks);

        parkingService.update(parking);

        //System.out.println(jsonObject);

        return "";
    }

    @ResponseBody
    @RequestMapping("deleteByIds")
    public String deleteByIds(String ids){
        //System.out.println(ids);
        String[] lst = ids.split(",");
        for (int index = 0;index < lst.length;index++)
        {
            int id = Integer.valueOf(lst[index]);
            parkingService.delete(id);
            //ownerService.delete(id);
            //houseService.delete(id);
        }
        JSONObject json=new JSONObject();
        json.accumulate("code",200);
        return json.toString();
    }

}
